﻿#include "RankBaseManager.h"


RankBaseManager::RankBaseManager(uint32_t iRankType, uint32_t iMiscType, uint32_t iRankMaxNum)
	: _iRankType(iRankType)
	, _iMiscType(iMiscType)
	, _iRankMaxNum(iRankMaxNum)
{
}

RankBaseManager::~RankBaseManager()
{
}

//string RankBaseManager::getMiscKey()
//{
//	return MiscInfoManager::getKey(_iMiscType);
//}

map<JceData::CacheMiscRankKey, uint32_t>& RankBaseManager::getRankMap(uint32_t iSubId)
{
	JceData::CacheMisc& stCacheMisc = getCacheMisc();
	return stCacheMisc.stBaseRank.mRank;
}

bool RankBaseManager::checkRankKeyTied(const JceData::CacheMiscRankKey &stKey1, const JceData::CacheMiscRankKey &stKey2)
{
	return (stKey1.iRankParam1 == stKey2.iRankParam1) && (stKey1.iRankParam2 == stKey1.iRankParam2);
}

void RankBaseManager::updateRank(uint32_t iSubId, uint32_t iObjId, JceData::CacheMiscRankKey& stRankKey)
{
	map<JceData::CacheMiscRankKey, uint32_t>& mRank = getRankMap(iSubId);
	map<JceData::CacheMiscRankKey, uint32_t>::iterator it;

	// 如果存在删除旧排行
	for (it = mRank.begin(); it != mRank.end(); it++)
	{
		if (it->second == iObjId)
		{
			mRank.erase(it);
			break;
		}
	}

	// 如果已满 && 小于最小入排行参数
	if (mRank.size() >= _iRankMaxNum)
	{
		it = mRank.begin();
		if (it != mRank.end() && stRankKey < it->first)
		{
			return;
		}
	}

	// 插入新记录
	mRank[stRankKey] = iObjId;

	// 如果人数超过限制, 删除第一个最低排行
	while (mRank.size() > _iRankMaxNum)
	{
		it = mRank.begin();
		if (it != mRank.end())
		{
			mRank.erase(it);
		}
	}

	//MISC_INFO_MGR->setChanged(getMiscKey());
	//ROLL_LOG_INFO("_iRankType: " << _iRankType << ", iMiscType: " << _iMiscType << ", stCacheMisc:\n" << TJceHelper::displayJce(stCacheMisc));
}

void RankBaseManager::getRankList(uint32_t iSubId, uint32_t iStartRank, uint32_t iRankNum, vector<RankData>& vRankData)
{
	map<JceData::CacheMiscRankKey, uint32_t>& mRank = getRankMap(iSubId);

	if (mRank.empty())
	{
		return;
	}

	uint32_t iRankId = 1;
	uint32_t iCurrNum = 0;
	map<JceData::CacheMiscRankKey, uint32_t>::reverse_iterator it = mRank.rbegin();

	for (; it != mRank.rend(); it++)
	{
		if (iRankId < iStartRank)
		{
			++iRankId;
			continue;
		}

		RankData stRankData(iRankId, it->second, it->first);
		vRankData.push_back(stRankData);
		//ROLL_LOG_DEBUG("iRankId: " << iRankId << ", it->first.iRankParam: " << it->first.iRankParam << ", it->first.iCharId: " << it->first.iCharId);

		++iRankId;
		++iCurrNum;
		if (iCurrNum >= iRankNum)
		{
			break;
		}
	}
}

void RankBaseManager::getTiedRankList(uint32_t iSubId, uint32_t iMaxRankId, uint32_t iMaxRankNum, vector<RankData>& vRankData)
{
	map<JceData::CacheMiscRankKey, uint32_t>& mRank = getRankMap(iSubId);
	map<JceData::CacheMiscRankKey, uint32_t>::reverse_iterator it;

	if (mRank.empty())
	{
		return;
	}

	it = mRank.rbegin();
	uint32_t iCurrRankId = 0;
	JceData::CacheMiscRankKey stRankKey;
	uint32_t iCurrNum = 0;
	for (; it != mRank.rend(); it++)
	{
		if (!checkRankKeyTied(stRankKey, it->first))
		{
			iCurrRankId++;
			stRankKey = it->first;

			if (iMaxRankId != 0 && iCurrRankId > iMaxRankId)
			{
				break;
			}
		}

		RankData stRankData(iCurrRankId, it->second, it->first);
		vRankData.push_back(stRankData);

		iCurrNum++;

		if (iMaxRankNum != 0 && iCurrNum >= iMaxRankNum)
		{
			break;
		}
	}
}

RankData RankBaseManager::getSelfRank(uint32_t iSubId, uint32_t iObjId)
{
	map<JceData::CacheMiscRankKey, uint32_t>& mRank = getRankMap(iSubId);
	map<JceData::CacheMiscRankKey, uint32_t>::reverse_iterator it;

	it = mRank.rbegin();
	uint32_t iRankId = 1;
	for (; it != mRank.rend(); it++)
	{
		if (it->second == iObjId)
		{
			return RankData(iRankId, it->second, it->first);
		}

		iRankId++;
	}

	return RankData();
}

RankData RankBaseManager::getTiedSelfRank(uint32_t iSubId, uint32_t iObjId)
{
	map<JceData::CacheMiscRankKey, uint32_t>& mRank = getRankMap(iSubId);
	map<JceData::CacheMiscRankKey, uint32_t>::reverse_iterator it;

	it = mRank.rbegin();
	uint32_t iCurrRankId = 0;
	JceData::CacheMiscRankKey stRankKey;
	for (; it != mRank.rend(); it++)
	{
		// 检查同名
		if (!checkRankKeyTied(stRankKey, it->first))
		{
			iCurrRankId++;

			stRankKey = it->first;
		}

		if (it->second == iObjId)
		{
			return RankData(iCurrRankId, it->second, it->first);
		}
	}

	return RankData();
}

void RankBaseManager::delFromRank(uint32_t iSubId, uint32_t iObjId)
{
	map<JceData::CacheMiscRankKey, uint32_t>& mRank = getRankMap(iSubId);
	map<JceData::CacheMiscRankKey, uint32_t>::iterator it;

	it = mRank.begin();
	for (; it != mRank.end(); it++)
	{
		if (it->second == iObjId)
		{
			mRank.erase(it);
			//MISC_INFO_MGR->setChanged(getMiscKey());
			break;
		}
	}
}

void RankBaseManager::clearRankList()
{
	map<JceData::CacheMiscRankKey, uint32_t>& mRank = getRankMap(0);
	mRank.clear();
	//MISC_INFO_MGR->setChanged(getMiscKey());
}